#!/usr/bin/perl -w  # useful for syntax highlighting!
package clreqd_modules;

use strict;
use cldb;
use Data::Dumper;
use clstatdb;
use ipinterfaces;
use Config;
use clnet qw(
		CLNET2_gen_ip_list
);

sub get_app_ips {
my %ARGS=@_;
my $rc;

	my $APP=$ARGS{APPLICATION};

	#################################################################
	# Get the details defined in the application configuration	#
	# records for this application.					#
	#################################################################

	my $CLDB=new cldb("$::ENV{TRUECL_CFG}/config.db");
	if(!defined($CLDB)) {
		return "ERROR - Unable to access the 'config' database.";
	}
	my $res=$CLDB->get_rows("SELECT * FROM applications WHERE name = '$APP';");
	if(!defined($res->[0]->{name})) {
		return "OK 1\nERROR Application '$APP' is not known on this node.";
	}
	my $CLSTATDB=new clstatdb("$::ENV{TRUECL_CFG}/status.db");
	if(!defined($CLSTATDB)) {
		return "ERROR - Unable to access the 'status' database.";
	}
	$res=$CLSTATDB->get_rows("SELECT * FROM app_status WHERE name = '$APP';");
	if(!defined($res->[0]->{name})) {
		return "OK 0 0"; # application is not running, so can not get results
	}
	if(!defined($res->[0]->{host}) || $res->[0]->{host} ne hostname) {
		return "ERROR - Application '$APP' not running on this host.";
	}
	if(!defined($res->[0]->{status}) || $res->[0]->{status} ne "RUNNING") {
		return "ERROR - Application '$APP' not in a 'RUNNING' state.";
	}
	$CLSTATDB->disconnect;
	$CLSTATDB=undef;
	$res=$CLDB->get_rows("SELECT * FROM application_ips WHERE name = '$APP';");
	$CLDB->disconnect; $CLDB=undef;
	if(!defined($res->[0]->{name})) {
		# OK return value - but 0 IP's allocated, 0 unallocated
		return "OK 0 0";
	}

	#################################################################
	# Generate a list of networks for address assignment.		#
	#################################################################

	my %ips=();
	my $ipinters=new ipinterfaces();
	my $c_alloc=0;
	my $c_unalloc=0;
	for my $crec (@$res) {
		my $net=$crec->{network};
		$net="?not_defined?" if !defined($net);
		my $ip_txt=$crec->{ips};
		my $version=$crec->{version};
		$version="4" if !defined($version);
		my ($_ips,$netmasks);
		($_ips,$netmasks)=CLNET2_gen_ip_list($ip_txt);
		my @I=split(/,/,$_ips);
		# Foreach IP we work out whether it is allocated or not...
		for my $cip (@I) {
			my $cint=$ipinters->get_interface(ip=>$cip);
			if(!defined($cint)) {
				$ips{$cip}={topology=>$crec->{network},version=>$version,status=>0,interface=>""};
				$c_unalloc++;
			} else {
				$ips{$cip}={topology=>$crec->{network},version=>$version,status=>1,interface=>$cint};
				$c_alloc++;
			}
		}
	}

	# We have details of all IP addresses so dump them in the form
	# of IP|topology|status|interface - ordered by topology/ip

	my $s="OK $c_alloc $c_unalloc\n";
	my @Ss=();
	for my $cip (sort {
			if($ips{$a}->{topology} ne $ips{$b}->{topology}) {
				return $ips{$a}->{topology} cmp $ips{$b}->{topology};
			}
			return $a cmp $b;
			} keys(%ips)) {
		if($ips{$cip}->{status}==0) {
			push @Ss,sprintf("%s|%s|0| ",$cip,$ips{$cip}->{topology});
		} else {
			push @Ss,sprintf("%s|%s|1|%s",$cip,$ips{$cip}->{topology},$ips{$cip}->{interface});
		}
	}
	$s.=join("\n",@Ss);
	return $s;
}

1;
